package cn.iocoder.yudao.module.alipay.controller.admin.costoutapply;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.alipay.controller.admin.costoutapply.vo.*;
import cn.iocoder.yudao.module.alipay.dal.dataobject.costoutapply.CostOutApplyDO;
import cn.iocoder.yudao.module.alipay.convert.costoutapply.CostOutApplyConvert;
import cn.iocoder.yudao.module.alipay.service.costoutapply.CostOutApplyService;

@Tag(name = "管理后台 - 提现记录")
@RestController
@RequestMapping("/alipay/cost-out-apply")
@Validated
public class CostOutApplyController {

    @Resource
    private CostOutApplyService costOutApplyService;

    @PostMapping("/create")
    @Operation(summary = "创建提现记录")
    @PreAuthorize("@ss.hasPermission('alipay:cost-out-apply:create')")
    public CommonResult<Long> createCostOutApply(@Valid @RequestBody CostOutApplyCreateReqVO createReqVO) {
        return success(costOutApplyService.createCostOutApply(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新提现记录")
    @PreAuthorize("@ss.hasPermission('alipay:cost-out-apply:update')")
    public CommonResult<Boolean> updateCostOutApply(@Valid @RequestBody CostOutApplyUpdateReqVO updateReqVO) {
        costOutApplyService.updateCostOutApply(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除提现记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('alipay:cost-out-apply:delete')")
    public CommonResult<Boolean> deleteCostOutApply(@RequestParam("id") Long id) {
        costOutApplyService.deleteCostOutApply(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得提现记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('alipay:cost-out-apply:query')")
    public CommonResult<CostOutApplyRespVO> getCostOutApply(@RequestParam("id") Long id) {
        CostOutApplyDO costOutApply = costOutApplyService.getCostOutApply(id);
        return success(CostOutApplyConvert.INSTANCE.convert(costOutApply));
    }

    @GetMapping("/list")
    @Operation(summary = "获得提现记录列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('alipay:cost-out-apply:query')")
    public CommonResult<List<CostOutApplyRespVO>> getCostOutApplyList(@RequestParam("ids") Collection<Long> ids) {
        List<CostOutApplyDO> list = costOutApplyService.getCostOutApplyList(ids);
        return success(CostOutApplyConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/getListByUserId")
    @Operation(summary = "根据userid获得提现记录列表")
    @Parameter(name = "userId", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('alipay:cost-out-apply:query')")
    public CommonResult<List<CostOutApplyRespVO>> getListByUserId(@RequestParam("userId") String userId) {
        List<CostOutApplyDO> list = costOutApplyService.getListByUserId(userId);
        return success(CostOutApplyConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得提现记录分页")
    @PreAuthorize("@ss.hasPermission('alipay:cost-out-apply:query')")
    public CommonResult<PageResult<CostOutApplyRespVO>> getCostOutApplyPage(@Valid CostOutApplyPageReqVO pageVO) {
        PageResult<CostOutApplyDO> pageResult = costOutApplyService.getCostOutApplyPage(pageVO);
        return success(CostOutApplyConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出提现记录 Excel")
    @PreAuthorize("@ss.hasPermission('alipay:cost-out-apply:export')")
    @OperateLog(type = EXPORT)
    public void exportCostOutApplyExcel(@Valid CostOutApplyExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<CostOutApplyDO> list = costOutApplyService.getCostOutApplyList(exportReqVO);
        // 导出 Excel
        List<CostOutApplyExcelVO> datas = CostOutApplyConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "提现记录.xls", "数据", CostOutApplyExcelVO.class, datas);
    }

}
